/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.mianshi.facejava.modules.oss.cloud;


import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;

import java.io.File;
import java.io.InputStream;

/**
 * 腾讯云存储
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-26 20:51
 */
public class QcloudCloudStorageService extends CloudStorageService {
	private COSClient client;

	public QcloudCloudStorageService(CloudStorageConfig config) {
		this.config = config;

		//初始化
		init();
	}

	private void init() {
		// 1 初始化用户身份信息(secretId, secretKey)
		COSCredentials cred = new BasicCOSCredentials("AKIDmLgmcCu59bnpb7zN7bpnY2GFzUkkxyCB", "tlOnxtc7NO9etsa2EAqHYZKp43oKbdYH");
// 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
		ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
// 3 生成cos客户端
		client = new COSClient(cred, clientConfig);
	}

	@Override
	public String upload(File file, String path) {
		//腾讯云必需要以"/"开头
		if (!path.startsWith("/")) {
			path = "/" + path;
		}

// 指定要上传到 COS 上对象键
// 对象键（Key）是对象在存储桶中的唯一标识。例如，在对象的访问域名 `bucket1-1250000000.cos.ap-guangzhou.myqcloud.com/doc1/pic1.jpg` 中，对象键为 doc1/pic1.jpg, 详情参考 [对象键](https://cloud.tencent.com/document/product/436/13324)
		String key = path;
		PutObjectRequest putObjectRequest = new PutObjectRequest(config.getQcloudBucketName(), key, file);
		PutObjectResult putObjectResult = client.putObject(putObjectRequest);
		return config.getQcloudDomain() + path;
	}


	@Override
	public String upload(byte[] data, String path) {
		return null;
	}

	@Override
	public String uploadSuffix(byte[] data, String suffix) {
		return null;
	}

	@Override
	public String uploadSuffix(File file, String suffix) {
		return upload(file, getPath(config.getQcloudPrefix(), suffix));
	}

	@Override
	public String upload(InputStream inputStream, String path) {
		return null;
	}

	@Override
	public String uploadSuffix(InputStream inputStream, String suffix) {
		return null;
	}
}
